package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.huibao.hulibao.R;
import com.huibao.hulibao.constant.GlobalConstants;
import com.huibao.hulibao.entity.LoginEntity;
import com.huibao.hulibao.entity.WebEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class WebLvAdapter extends BaseAdapter {
    private List<WebEntity.DetailData> mDatas;
    private Context mContext;
    private LayoutInflater mInflater;

    public WebLvAdapter(List<WebEntity.DetailData> datas, Context context) {
        this.mDatas = datas;
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        WebEntity.DetailData wedd = mDatas.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_web, parent, false);
            holder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_content.setText(wedd.name);
        String icon = wedd.icon;
        if (icon.contains("http://") || icon.contains("https://")) {
            Glide.with(mContext).load(icon).into(holder.iv_pic);
        } else {
            // LogUtils.d("url = " + GlobalConstants.HOST + icon);
            Glide.with(mContext).load(GlobalConstants.HOST + icon).into(holder.iv_pic);
        }

        return convertView;
    }

    static class ViewHolder {


        @BindView(R.id.iv_pic)
        ImageView iv_pic;
        @BindView(R.id.tv_content)
        TextView tv_content;

        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
